﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Text;

using ShopModel;

namespace ShopDAO
{
    public class VideoDAO
    {
        private string connectionString;

        public VideoDAO()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csShop"].ConnectionString;
        }

        /// <summary>
        /// Zapisuje informacje o pliku video do bazy.
        /// (Saves information about the video file to the database.)
        /// </summary>
        /// <param name="Video">
        /// Obiekt VideoInfo zawierający informacje o wybranym pliku video.
        /// (VideoInfo object containing information about the selected video file.)
        /// </param>
        /// <returns>
        /// Zwraca 'true' jeśli dodawanie do bazy zakonczyło się sukcesem.
        /// (Returns 'true' if adding to the database succeeded.)
        /// </returns>
        public  bool SaveVideo(VideoInfo Video)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();

            sqlCommand.CommandText = "SaveVideo";
            sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

            sqlCommand.Parameters.Add(new SqlParameter("@FileName", Video.FileName));
            sqlCommand.Parameters.Add(new SqlParameter("@ProductID", Video.ProductID));
            sqlCommand.Parameters.Add(new SqlParameter("@Label", Video.Label));


            try
            {
                connection.Open();
                return sqlCommand.ExecuteNonQuery() > 0;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Pobiera listę filmów dla danego produktu.
        /// (Downloads list of movies for the given product.)
        /// </summary>
        /// <param name="ProductID">
        /// Unikalne ID produktu.
        /// (Unique product ID.)
        /// </param>
        /// <returns>
        /// Zwraca listę obiektów VideoInfo zawierająca informacje o wszystkich plikach wideo przypisanych do danego produktu.
        /// (Returns list of VideoInfo objects containing information about all videos assigned to the given product.)
        /// </returns>
        public  List<VideoInfo> GetVideos(int ProductID)
        {
            SqlConnection sqlC = new SqlConnection(connectionString);
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;

            List<VideoInfo> result = new List<VideoInfo>();

            command.CommandText = "GetVideos";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@ProductID", ProductID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                while (sqlR.Read())
                {
                    result.Add(new VideoInfo((int)sqlR["IDVideo"], (int)sqlR["ProductID"], (string)sqlR["FileName"], (string)sqlR["Label"]));
                }
                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return null;
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// Usuwa informacje o wybranym pliku video z bazy.
        /// (Removes information about the selected video file from the database.)
        /// </summary>
        /// <param name="VideoID">
        /// Unikalne ID pliku video.
        /// (Unique ID video.)
        /// </param>
        /// <returns>
        /// Zwraca 'true' jeśli usuwanie danych z bazy zakonczyło się sukcesem.
        /// (Returns 'true' if the removal of data from the database succeeded.)
        /// </returns>
        public  bool DeleteVideo(int VideoID)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();

            sqlCommand.CommandText = "DeleteVideo";
            sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

            sqlCommand.Parameters.Add(new SqlParameter("@VideoID", VideoID));

            try
            {
                connection.Open();
                return (int)sqlCommand.ExecuteScalar() > 0;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Pobiera informacje o wybranym pliku video.
        /// (Retrieves information about the selected video file.)
        /// </summary>
        /// <param name="VideoID">
        /// Unikalne ID pliku video.
        /// (Unique ID video.)
        /// </param>
        /// <returns>
        /// Zwraca obiekt VideoInfo z informacjami na temat wybranego pliku video.
        /// (Returns VideoInfo object with information about the selected video file.)
        /// </returns>
        public VideoInfo GetVideo(int VideoID)
        {
            SqlConnection sqlC = new SqlConnection(connectionString);
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;

            VideoInfo result = null;

            command.CommandText = "GetVideos";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@VideoID", VideoID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                if (sqlR.Read())
                    result = new VideoInfo((int)sqlR["IDVideo"], (int)sqlR["ProductID"], (string)sqlR["FileName"], (string)sqlR["Label"]);
                
                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return null;
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// Removes video’s files from the database.
        /// </summary>
        /// <param name="productID">Product id for which we remove video.</param>
        /// <returns>Returns True jesli when removal succeeded.</returns>
        public bool DeleteVideoFromDataBase(int productID)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();



            sqlCommand.CommandText = "DeleteVideoFromDataBase";
            sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
            sqlCommand.Parameters.Add(new SqlParameter("@productID", productID));
            try
            {
                connection.Open();
                sqlCommand.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }

        }
    }
}
